package com.gitee.ricesugar.mall.model.Service;

import com.gitee.ricesugar.mall.model.DAO.ProductDAO;
import com.gitee.ricesugar.mall.model.POJO.DO.ProductDO;
import com.gitee.ricesugar.mall.model.POJO.DTO.ProductDTO;

import java.sql.SQLException;
import java.util.ArrayList;

public class ProductService {

    private ProductDAO productDAO;

    public ProductService() throws SQLException, ClassNotFoundException {
        productDAO = new ProductDAO();
    }

    public ArrayList<ProductDTO> listProducts() throws SQLException {

        ArrayList<ProductDO> productDOs = productDAO.listProducts();
        ArrayList<ProductDTO> productDTOs = new ArrayList<>();

        for (ProductDO productDO : productDOs){
            int product_id = productDO.getProduct_id();
            String name = productDO.getName();
            double price = productDO.getPrice();
            int stock = productDO.getStock();
            int sales = productDO.getSales();
            String img = productDO.getImg();
            productDTOs.add(new ProductDTO(product_id, name, price, stock, sales, img));
        }

        return productDTOs;
    }

    public ProductDTO getProduct(String pid) throws SQLException {
        ProductDO productDO = productDAO.getProduct(pid);
        ProductDTO productDTO = null;

        int id = productDO.getProduct_id();
        String name = productDO.getName();
        double price = productDO.getPrice();
        int stock = productDO.getStock();
        int sales = productDO.getSales();
        String img = productDO.getImg();

        productDTO = new ProductDTO(id, name, price, stock, sales, img);

        return productDTO;
    }
}
